package km.TestContact;
import java.util.*;

import android.app.*;
import android.content.*;
import android.os.*;
import android.provider.*;
import android.util.*;
import android.view.*;
import android.widget.*;
public final class TestAdd extends Activity {   
	public static final String TAG = "ContactsAdder";   
	public static final String ACCOUNT_NAME =    "com.example.android.contactmanager.ContactsAdder.ACCOUNT_NAME"; 
	public static final String ACCOUNT_TYPE =            "com.example.android.contactmanager.ContactsAdder.ACCOUNT_TYPE"; 
	private Button mContactSaveButton;   
	private EditText edName;
	private EditText edCompany;
	private EditText edGrade;
	private EditText edMail;
	private EditText edPhone;
	private EditText edTel;
	private EditText edFax;  
	/**     * Called when the activity is first created. Responsible for initializing the UI.     */ 
	@Override    public void onCreate(Bundle savedInstanceState)    {    
		Log.v(TAG, "Activity State: onCreate()");      
		super.onCreate(savedInstanceState);      
		setContentView(R.layout.test_adder);  
		// Obtain handles to UI objects    
		edName = (EditText) findViewById(R.id.ed_name);   
		edCompany = (EditText) findViewById(R.id.ed_company);   
		edGrade = (EditText) findViewById(R.id.ed_grade);   
		edMail = (EditText) findViewById(R.id.ed_mail);   
		edPhone = (EditText) findViewById(R.id.ed_phone);   
		edTel = (EditText) findViewById(R.id.ed_tel);   
		edFax = (EditText) findViewById(R.id.ed_fax);   
		mContactSaveButton = (Button) findViewById(R.id.btn_save);     
		
//		mContactPhoneTypes = new ArrayList<Integer>();    
//		mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_HOME);   
//		mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_WORK);  
//		mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);  
//		mContactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_OTHER);    
//		mContactEmailTypes = new ArrayList<Integer>();      
//		mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_HOME);    
//		mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_WORK);  
//		mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_MOBILE);  
//		mContactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_OTHER);		
				
		mContactSaveButton.setOnClickListener(new View.OnClickListener() {     
			public void onClick(View v) {           
				onSaveButtonClicked();        
				}    
   });  
		} 
  /**    
   * Actions for when the Save button is clicked. Creates a contact entry and terminates the   
	* activity.     */ 
	private void onSaveButtonClicked() {    
		Log.v(TAG, "Save button clicked");  
		createContactEntry();      
		finish();   
		}    
	/**    
	 * Creates a contact entry from the current UI values in the account named by mSelectedAccount.  
	 *    */  
	protected void createContactEntry() { 
		// Get values from UI     
		String name = edName.getText().toString();      
		String phone = edPhone.getText().toString();   
		String email = edMail.getText().toString();   
		String company = edCompany.getText().toString();   
		String fax = edFax.getText().toString();   
		String grade = edGrade.getText().toString();   
		String tel = edTel.getText().toString();   
				ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();  
				ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)  
//						.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, mSelectedAccount.getType()) 
//						.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, mSelectedAccount.getName())  
						.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "") 
						.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "")  
						.build());
				//이름
				ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)   
						.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)     
						.withValue(ContactsContract.Data.MIMETYPE,                     
								ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)   
								.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name) 
								.build());
				//회사명 , 직위
				ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)   
						.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)     
						.withValue(ContactsContract.Data.MIMETYPE,                     
								ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)   
								.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company)
								.withValue(ContactsContract.CommonDataKinds.Organization.TITLE, grade)
								.withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
								.build());
				//메일주소
				ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)   
						.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)      
						.withValue(ContactsContract.Data.MIMETYPE,                    
								ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)  
								.withValue(ContactsContract.CommonDataKinds.Email.DATA, null)  
								.withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)    
								.build());
				//휴대전화
				ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)  
						.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)      
						.withValue(ContactsContract.Data.MIMETYPE,                    
								ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)  
								.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)   
								.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) 
								.build());    
				//회사전화
				ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)  
						.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)      
						.withValue(ContactsContract.Data.MIMETYPE,                    
								ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)  
								.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, tel)   
								.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK) 
								.build());    
				//팩스
				ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)  
						.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)      
						.withValue(ContactsContract.Data.MIMETYPE,                    
								ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)  
								.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, fax)   
								.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK) 
								.build());    
				
//				ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)   
//						.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
//						.withValue(ContactsContract.Data.MIMETYPE,                    
//								ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)  
//								.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "5555")   
//								.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 7) //ggg     
//								.build()); 
				
				
				Log.i(TAG,"Creating contact: " + name);   
						try {     
							getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);    
							} catch (Exception e) {    
								// Display warning       
								Context ctx = getApplicationContext();  
								CharSequence txt = getString(R.string.contactCreationFailure);   
								int duration = Toast.LENGTH_SHORT;        
								Toast toast = Toast.makeText(ctx, txt, duration); 
								toast.show();         
								// Log exception         
								Log.e(TAG, "Exceptoin encoutered while inserting contact: " + e);   
								}   
							}  
	/**     * Called when this activity is about to be destroyed by the system.     */   
	@Override    public void onDestroy() {      
		// Remove AccountManager callback     
		super.onDestroy();   
		}
}
